<?php
namespace app\api\controller;

use think\Db;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class Tq
{

    private static function surfNet($query) {
       $headers = [
                'headers' => [
                    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                    'Accept-Encoding' => 'gzip, deflate, br',
                    'Accept-Language' => 'zh-CN,zh;q=0.8',
                    'Cache-Control' => 'no-cache',
                    'Host' => 'www.tianqi.com',
                    'User-Agent' => 'Mozilla/5.0 (Linux; U; Android 4.4.3; zh-cn; HTC 802w Build/KTU84L) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025489 Mobile Safari/533.1 MicroMessenger/6.3.13.49_r4080b63.740 NetType/WIFI Language/zh_CN',
                    'Connection' => 'keep-alive',
                    'upgrade-insecure-requests' => 1,
                    'Pragma' => 'no-cache',
                    'Referer' => 'http://www.tianqi.com/chinacity.html'
                ]
        ];

        $client = new Client();

        $response= $client->request('GET',$query['url'],$headers);

        if($response->getStatusCode() == 200){
            return $response->getBody()->getContents();
        }else{
            return false;
        }
    }


    public function index(){
        $userSelect = Db::name('uwrela')->alias('u')->field('u.*,c.link')
            ->leftJoin('city_tq c','u.tid=c.id')->where('u.id','>',0)->select();

        foreach ($userSelect as $v){
            $param=[];
            $param['url']='http://www.tianqi.com'.$v['link'];
            $content = self::surfNet($param);

            $crawler = new Crawler($content);
            $address = $crawler->filter('.weather_info .name h2')->text();
            $week = $crawler->filter('.weather_info .week')->text();
            $now = $crawler->filter('.weather_info .weather .now')->text();
            $today = $crawler->filter('.weather_info .weather span')->text();

            $shidu='';
            $crawler->filter('.weather_info .shidu b')->each(function (Crawler $node, $i) use (&$shidu){
                    if($i==0){
                        $shidu=$node->text()."\t";
                    }else{
                        $shidu .= $node->text()."\t";
                    }
            });

            $kongqi = $crawler->filter('.weather_info .kongqi h5')->text();
            $pm = $crawler->filter('.weather_info .kongqi h6')->text();
            $sun = $crawler->filter('.weather_info .kongqi span')->text();

            $life='';
            $crawler->filter('.weather_life300 li')->each(function (Crawler $node, $i) use (&$life){
                    if($i==0){
                        $life="\t\t".$node->filter('b')->text().'：'.$node->filter('p')->text()."\n";
                    }else{
                        $life.="\t\t".$node->filter('b')->text().'：'.$node->filter('p')->text()."\n";
                    }
            });

            $weather =$week."\n".$address."\t\t".'现在温度：'.$now."\n".'今天天气：'.$today."\n".$shidu."\n".$kongqi."\t".$pm."\n".$sun."\n\n"."今天生活指数：\n".$life;

            $crawler->clear();
            Db::name('weather_mess')->insert(['wearherInfo'=>$weather,'username'=>$v['nickname'],'createtime'=>date('Y-m-d H:i:s'),'uid'=>$v['id']]);

            sleep(1);
        }

        echo '完成';

    }


}
